package cn.lzd.ui.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;
import cn.lzd.ui.R;
import cn.lzd.ui.utils.GlideUtil;
import com.blankj.utilcode.util.SizeUtils;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;

/**
 * icon text view，用于上方显示一个 url 指向的图片，下方显示文字的情景
 *
 * @author lizhidong
 */
public class IconWithBottomTextView extends ConstraintLayout {
    private final TextView mTextView;
    private final RoundImageView mImageView;

    public IconWithBottomTextView(Context context) {
        this(context, null);
    }

    public IconWithBottomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(R.styleable.IconWithBottomTextView);
        String text = typedArray.getString(R.styleable.IconWithBottomTextView_iwbt_text);
        int textColor = typedArray.getColor(R.styleable.IconWithBottomTextView_iwbt_textColor,
                ContextCompat.getColor(context, R.color.base_ui_gray));
        int textSize = typedArray.getDimensionPixelSize(R.styleable.IconWithBottomTextView_iwbt_textSize,
                SizeUtils.sp2px(14));
        int iconSize = typedArray.getDimensionPixelSize(R.styleable.IconWithBottomTextView_iwbt_iconSize,
                SizeUtils.dp2px(40));
        typedArray.recycle();

        mImageView = new RoundImageView(context);
        mImageView.setId(View.generateViewId());
        LayoutParams imgParams = new LayoutParams(iconSize, iconSize);
        imgParams.topToTop = LayoutParams.PARENT_ID;
        imgParams.startToStart = LayoutParams.PARENT_ID;
        imgParams.endToEnd = LayoutParams.PARENT_ID;
        addView(mImageView, imgParams);

        mTextView = new TextView(context);
        mTextView.setText(text);
        mTextView.setSingleLine();
        mTextView.setGravity(Gravity.CENTER);
        mTextView.setEllipsize(TextUtils.TruncateAt.END);
        mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
        mTextView.setTextColor(textColor);
        LayoutParams textParams = new LayoutParams(MATCH_PARENT, WRAP_CONTENT);
        textParams.topToBottom = mImageView.getId();
        textParams.topMargin = SizeUtils.dp2px(2);
        addView(mTextView, textParams);
    }

    public void setText(String text) {
        mTextView.setText(text);
    }

    public void setTextSize(int sizeResourceId) {
        mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX,
                getContext().getResources().getDimensionPixelSize(sizeResourceId));
    }

    public void setIconSize(int sizeResourceId) {
        setIconSize(sizeResourceId, sizeResourceId);
    }

    public void setIconSize(int widthResourceId, int heightResourceId) {
        int width = getContext().getResources().getDimensionPixelSize(widthResourceId);
        int height = getContext().getResources().getDimensionPixelSize(heightResourceId);
        mImageView.getLayoutParams().width = width;
        mImageView.getLayoutParams().height = height;
        mImageView.requestLayout();
    }

    public void setCornerSize(int cornerResourceSize) {
        mImageView.setRadius(getContext().getResources().getDimensionPixelSize(cornerResourceSize));
    }

    public void showIcon(String url) {
        GlideUtil.loadUrl2Img(url, mImageView);
    }

    public void setSingleLine(boolean singleLine) {
        mTextView.setSingleLine(singleLine);
    }

    public interface IDataProvider {
        String getImgUrl();

        String getText();

        default View.OnClickListener onClick() {
            return null;
        }
    }
}
